package com.zjmzxfzhl.modules.monitor.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.annotation.Resource;

import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.stereotype.Service;

import com.zjmzxfzhl.common.util.CommonUtil;
import com.zjmzxfzhl.modules.monitor.vo.RedisInfo;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class RedisService {

	@Resource
	private RedisConnectionFactory redisConnectionFactory;

	public List<RedisInfo> getInfo() {
		Properties info = redisConnectionFactory.getConnection().info();
		List<RedisInfo> infoList = new ArrayList<>();
		RedisInfo redisInfo = null;
		for (Map.Entry<Object, Object> entry : info.entrySet()) {
			redisInfo = new RedisInfo();
			if (CommonUtil.isNotEmptyObject(entry.getKey())) {
				redisInfo.setKey((String) entry.getKey());
				redisInfo.setValue((String) entry.getValue());
				infoList.add(redisInfo);
			}
		}
		return infoList;
	}

	public Map<String, Object> getKeysSize() {
		Long dbSize = redisConnectionFactory.getConnection().dbSize();
		Map<String, Object> map = new HashMap<>();
		map.put("time", System.currentTimeMillis());
		map.put("dbSize", dbSize);

		log.info("--getKeysSize--: " + map.toString());
		return map;
	}

	public Map<String, Object> getMemoryInfo() {
		Map<String, Object> map = null;
		Properties info = redisConnectionFactory.getConnection().info();
		for (Map.Entry<Object, Object> entry : info.entrySet()) {
			String key = (String) entry.getKey();
			if ("used_memory".equals(key)) {
				map = new HashMap<>();
				map.put("used_memory", entry.getValue());
				map.put("time", System.currentTimeMillis());
			}
		}
		log.info("--getMemoryInfo--: " + map.toString());
		return map;
	}
}
